package com.gmadmin.configuration.secutity.config;

import com.gmadmin.common.HttpStatus;
import com.gmadmin.utils.ResponseUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.MessageFormat;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2022/6/16 14:07
 */
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        String msg = MessageFormat.format("请求访问 {},没有访问权限，无法访问系统资源", request.getRequestURI());

        ResponseUtil.renderJson(response, HttpStatus.FORBIDDEN,msg);
    }
}
